package com.example.springbootredissession1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    /**
     * 主页
     * @param request
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request) {
        String msg = "欢迎进入主页";
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            msg = "未登录！";
        }
        return msg;
    }

    /**
     * 登陆
     * @param request
     * @return
     */
    @RequestMapping(value = "/login")
    public String login(HttpServletRequest request) {
        String msg = "logon failure!";
        request.getSession().setAttribute("user", "admin");
        msg = "登陆成功!";
        return msg;
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @RequestMapping(value = "/loginout")
    public String loginout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "退出成功！";
    }

}